import 'package:flutter/widgets.dart';
import 'package:get/get.dart';

// AppLifecycleService 用于监听应用的生命周期状态
class AppLifecycleService extends GetxService with WidgetsBindingObserver {
  final RxBool isAppInForeground = true.obs;

  @override
  void onInit() {
    super.onInit();
    WidgetsBinding.instance.addObserver(this); // 注册监听
  }

  @override
  void onClose() {
    WidgetsBinding.instance.removeObserver(this); // 移除监听
    super.onClose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    switch (state) {
      case AppLifecycleState.resumed:
        isAppInForeground.value = true; // 应用进入前台
        break;
      case AppLifecycleState.paused:
      case AppLifecycleState.inactive:
      case AppLifecycleState.detached:
      case AppLifecycleState.hidden:
        isAppInForeground.value = false; // 应用退到后台
        break;
    }
  }
}
